package springtest.async.controller;

import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncTask;

import springtest.async.pojo.TestResult;
import springtest.async.service.TestAsyncService;
import springtest.async.service.TestCallback;

/***
 * 
 * @author chenzhiqiang 2018-02-16 11:02:12
 */
@RestController
public class AsyncController {
	@Autowired
	private TestAsyncService testService;
	TestResult result = new TestResult();

	@RequestMapping("/async/test")
	@ResponseBody
	// 保存数据
	public Callable<String> testAsync(HttpServletRequest request) throws Exception {
		System.out.println("controller调用！thread id is" + Thread.currentThread().getId());
		Callable<String> asyncTask = new Callable<String>() {
			public String call() throws Exception {
				// Thread.sleep(3000);调用Service进行数据导入
				String value = testService.testAsync(result);
				System.out.println("异步完成！thread id is" + Thread.currentThread().getId());
				return value;
			}
		};
		return asyncTask;
	}

	@RequestMapping("/async/123")
	// 处理进度
	public String mvc12() {
		// 调用进度处理Service
		return "123";
	}

	@RequestMapping("/async/124")

	public String mvc13() throws Exception {
		Thread.sleep(5000);
		return "124";
	}

	// 测试Webasync
	@RequestMapping("/async/webasync")
	public WebAsyncTask<String> testWebAsyncTask() {
		Callable<String> callable = new Callable<String>() {
			@Override
			public String call() throws Exception {
				Thread.sleep(5000);// 假设为长时间处理任务
				return "异步执行完毕";// 超时
			}
		};
		WebAsyncTask<String> webAsyncTask = new WebAsyncTask<String>(1000, callable);
		webAsyncTask.onTimeout(new Callable<String>() {
			@Override
			public String call() throws Exception {
				// TODO Auto-generated method stub
				return "处理超时";
			}
		});
		return webAsyncTask;
	}

	@RequestMapping("/async/deferredResult")
	public DeferredResult<String> testDeferredResult() {
		DeferredResult<String> deferredResult = new DeferredResult<String>(1000l);
		System.out.println("controller调用的线程！thread id is" + Thread.currentThread().getId());
		testService.testDeferred(new TestCallback() {
			public void callback(Object result) {
				System.out.println("deferredResult异步调用完成的线程！thread id is" + Thread.currentThread().getId());
				deferredResult.setResult((String) result);
			}
		});
		// 超时处理
		deferredResult.onTimeout(new Runnable() {
			@Override
			public void run() {
				System.out.println("异步调用执行超时！thread id is : " + Thread.currentThread().getId());
				deferredResult.setResult("执行超时");
			}
		});
		return deferredResult;
	}

	private void dealInOtherThread(DeferredResult<String> deferredResult) {
		testService.testDeferred(new TestCallback() {

			@Override
			public void callback(Object result) {
				// TODO Auto-generated method stub
				deferredResult.setResult((String) result);
			}
		});
	}
}
